﻿// SDR_demo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <iio.h>
int receive(struct iio_context* ctx)
{
    struct iio_device* dev;
    struct iio_channel* rx0_i, * rx0_q;
    struct iio_buffer* rxbuf;

    dev = iio_context_find_device(ctx, "cf-ad9361-lpc");

    rx0_i = iio_device_find_channel(dev, "voltage0", 0);
    rx0_q = iio_device_find_channel(dev, "voltage1", 0);

    iio_channel_enable(rx0_i);
    iio_channel_enable(rx0_q);

    rxbuf = iio_device_create_buffer(dev, 4096, false);
    if (!rxbuf) {
        perror("Could not create RX buffer");
        exit(EXIT_FAILURE); // 使用标准的 exit 函数来退出
    }

    while (true) {
        void* p_dat, * p_end;
        ptrdiff_t p_inc;

        iio_buffer_refill(rxbuf);

        p_inc = iio_buffer_step(rxbuf);
        p_end = (void*)iio_buffer_end(rxbuf); // 确保 p_end 也是 void* 类型

        for (p_dat = (void*)iio_buffer_first(rxbuf, rx0_i); p_dat < p_end; p_dat = (void*)((char*)p_dat + p_inc)) {
            const int16_t* data = static_cast<const int16_t*>(p_dat);
            const int16_t i = data[0]; // Real (I)
            const int16_t q = data[1]; // Imag (Q)
            printf("%d\n", i);
            printf("%d\n", q);
            /* Process here */

        }
    }

    iio_buffer_destroy(rxbuf);
    return 0;
}
int main(int argc, char** argv)
{
	struct iio_context* ctx;
	struct iio_device* phy;

	ctx = iio_create_context_from_uri("ip:192.168.2.1");

	phy = iio_context_find_device(ctx, "ad9361-phy");

	iio_channel_attr_write_longlong(
		iio_device_find_channel(phy, "altvoltage0", true),
		"frequency",
		2400000000); /* RX LO frequency 2.4GHz */

	iio_channel_attr_write_longlong(
		iio_device_find_channel(phy, "voltage0", false),
		"sampling_frequency",
		5000000); /* RX baseband rate 5 MSPS */

	receive(ctx);

	iio_context_destroy(ctx);

	return 0;
}